package com.project.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.project.bean.SellBean;
import com.project.bean.SellProductBean;
import com.project.mapper.IProductMapper;
import com.project.mapper.ISellMapper;
import com.project.mapper.ISellProductMapper;
import com.project.service.ISellService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.util.List;


@Service
@Transactional
public class SellServiceImpl implements ISellService {
    @Autowired
    private ISellMapper sellMapper;
    @Autowired
    private IProductMapper productMapper;
    @Autowired
    private ISellProductMapper sellProductMapper;

    @Override
    public int[] addSell(SellBean sell, List<SellProductBean> infoList) {
        int[] arr = productMapper.findByNum(infoList);
        if( arr.length!= 0){
            return arr;
        }
        sellMapper.addSell(sell);
        sellProductMapper.addInfoList(sell.getId(),infoList);
        productMapper.sellProductList(infoList);
        return null;

    }

    @Override
    public PageInfo<SellBean> findByItem(int pn, int pageSize, String client, LocalDate startDate, LocalDate endDate) {
        PageHelper.startPage(pn,pageSize);
        return PageInfo.of(sellMapper.findByItem(client,startDate,endDate));
    }

    @Override
    public SellBean findById(int id) {
        return sellMapper.findById(id);
    }
}
